package artificialNeuralNetworks.ANN;

import java.util.LinkedHashSet;
import java.util.Random;

import debug.Dbg;
import decisiontreelearning.DecisionTree.Example;
import decisiontreelearning.DecisionTree.ExampleSet;

/**
 * FileName: ANNExampleSet.java
 * @Description:
 * 
 * @author Xunhu(Tiger) Sun
 *         email: TigerSun86@gmail.com
 * @date Mar 10, 2014 10:19:01 PM
 */
public class ANNExampleSet extends LinkedHashSet<ANNExample> {
    private static final long serialVersionUID = 1L;
    public static final String MODULE = "NES";
    public static final boolean DBG = true;

    /**
     * public ANNExampleSet[] splitIntoMultiSets(int numOfSets)
     * 
     * Splits given ExampleSet into 2 ExampleSets by given ratio in percentage.
     * The first output ExampleSet has the number of examples of the ratio, the
     * second one has remaining examples. Splits examples randomly.
     * 
     * @return: An array with 2 ExampleSets; null, if given ratio is not between
     *          0 and 1.
     */
    public ANNExampleSet[] splitIntoMultiSets (final int numOfSets) {
        if (numOfSets <= 0) {
            return null;
        }
        final int num;
        if (numOfSets > this.size()) {
            num = this.size();
        } else {
            num = numOfSets;
        }

        final ANNExampleSet[] exArray = new ANNExampleSet[num];
        for (int i = 0; i < num; i++) {
            exArray[i] = new ANNExampleSet();
        }
        int count = 0;
        for (ANNExample e : this) {
            final int index = count % num;
            exArray[index].add(e);
            count++;
        }

        Dbg.print(DBG, MODULE, "Splitted examples into " + num + " sub sets.");
        for (int i = 0; i < num; i++) {
            Dbg.print(DBG, MODULE, "Set " + i + " size " + exArray[i].size());
        }

        return exArray;
    }
    
    /**
     * public ANNExampleSet[] splitIntoTwoSets(double ratio)
     * 
     * Splits given ExampleSet into 2 ExampleSets by given ratio in percentage.
     * The first output ExampleSet has the number of examples of the ratio, the
     * second one has remaining examples. Splits examples randomly.
     * 
     * @return: An array with 2 ExampleSets; null, if given ratio is not between
     *          0 and 1.
     */
    public ANNExampleSet[] splitIntoTwoSets (final double ratio) {
        if (ratio > 1.0 || ratio < 0) {
            return null;
        }

        final ANNExampleSet[] exArray = new ANNExampleSet[2];
        exArray[0] = new ANNExampleSet();
        exArray[1] = new ANNExampleSet();

        final int numOfFirst = (int) (this.size() * ratio);
        int countFirst = 0;
        for (ANNExample e : this) {
            if (countFirst < numOfFirst){
                exArray[0].add(e);
                countFirst++;
            } else {
                exArray[1].add(e);
            }
        }

        return exArray;
    }
}
